//
//  WDADControlManager.m
//  WDPolymerizeSDK
//
//  Created by little People on 2023/6/2.
//

#import "WDADControlManager.h"
#import "WDNSLog.h"
#import "WDBMSModel.h"

@implementation WDADControlManager
{
    NSInteger splashAdCurrentCount;
//    NSInteger splashAdCurrentCount
}
static WDADControlManager *_adControl = nil;
+ (instancetype)shareADControlManager
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _adControl = [[self alloc] init];
        _adControl.interstitialAdCurrentCount = 0;
        _adControl.cardAdCurrentCount = 0;
    });
    return _adControl;
}

- (BOOL)canShowCard
{
    if ([WDBMSModel shareBMSModel].cardConfig && [WDBMSModel shareBMSModel].cardConfig.count > 0)
    {
        NSString *cardFirst = [NSString stringWithFormat:@"%@",[WDBMSModel shareBMSModel].cardConfig.firstObject];
        NSString *cardSecond = [NSString stringWithFormat:@"%@",[WDBMSModel shareBMSModel].cardConfig[1]];
        NSString *cardThird = [NSString stringWithFormat:@"%@",[WDBMSModel shareBMSModel].cardConfig[2]];
        
        if (![self cardCountVeryDay])
        {
            //达到每日上限
            return NO;
        }
        if (self.cardAdCurrentCount == 0)
        {
            [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"saveCardCount"];
            [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"CardTargetCount"];
        }
        self.cardAdCurrentCount++;
        if ([cardFirst isEqualToString:@"1"])
        {
            NSString *saveCardCount = [[NSUserDefaults standardUserDefaults] objectForKey:@"saveCardCount"];
            if (saveCardCount.length == 0)
            {
                WDLog(@"card ad 没有保存的参数，默认第一次");
                [[NSUserDefaults standardUserDefaults] setObject:@"1" forKey:@"saveCardCount"];
                [[NSUserDefaults standardUserDefaults] setObject:[NSString stringWithFormat:@"%ld",[cardSecond integerValue]+1] forKey:@"CardTargetCount"];
                if ([cardSecond integerValue] == 0)
                {
                    return YES;
                }
            }
            else{
                
                NSInteger newCount = [saveCardCount integerValue]+1;
                [[NSUserDefaults standardUserDefaults] setObject:[NSString stringWithFormat:@"%ld",newCount] forKey:@"saveCardCount"];
                NSString *currentCardCount = [[NSUserDefaults standardUserDefaults] objectForKey:@"saveCardCount"];
                NSString *CardTargetCount = [[NSUserDefaults standardUserDefaults] objectForKey:@"CardTargetCount"];
                [[NSUserDefaults standardUserDefaults] setObject:[NSString stringWithFormat:@"%ld",[cardThird integerValue]+newCount+1] forKey:@"CardTargetCount"];
                WDLog(@"当前是%@次，要在%ld次显示",currentCardCount,[CardTargetCount integerValue]);
                if ([cardThird integerValue] == 0)
                {
                    WDLog(@"但cardThird间隔为0，所以需要显示");
                    return YES;
                }
                if ([CardTargetCount integerValue] == [currentCardCount integerValue])
                {
                    
                    return YES;
                }
            }
        }
    }
    return NO;
}

- (BOOL)cardCountVeryDay
{
    NSString *cardFourth = [NSString stringWithFormat:@"%@",[WDBMSModel shareBMSModel].cardConfig[3]];
    NSString *cardCountVeryDay = [[NSUserDefaults standardUserDefaults] objectForKey:@"cardCountVeryDay"];
    if (cardCountVeryDay.length > 0)
    {
        NSInteger newCount = [cardCountVeryDay integerValue]+1;
        [[NSUserDefaults standardUserDefaults] setObject:[NSString stringWithFormat:@"%ld",newCount] forKey:@"cardCountVeryDay"];
    }
    else{
        cardCountVeryDay = @"0";
        [[NSUserDefaults standardUserDefaults] setObject:@"1" forKey:@"cardCountVeryDay"];
    }
    if ([cardFourth integerValue] > 0)
    {
        
        NSTimeInterval time = [[NSDate date] timeIntervalSince1970];
        NSDate *date = [NSDate dateWithTimeIntervalSince1970:time];
        NSDateFormatter *objDateformat = [[NSDateFormatter alloc] init];
        [objDateformat setDateFormat:@"yyyyMMdd"];
        NSString *currentDate = [objDateformat stringFromDate:date];
        
        NSString *saveDate = [[NSUserDefaults standardUserDefaults] objectForKey:@"cardSaveDate"];
        if (saveDate.length > 0)
        {
            WDLog(@"splash当前日期为%@,saveDate为%@,当前次数为%@,上限次数为%@",currentDate,saveDate,cardCountVeryDay,cardFourth);
            if ([saveDate integerValue] == [currentDate integerValue])
            {
                if ([cardCountVeryDay integerValue] <= [cardFourth integerValue])
                {
                    //还没到上限
                    return YES;
                }
                else
                {
                    return NO;
                }
            }else{
                //重置次数
                [[NSUserDefaults standardUserDefaults] setObject:@"0" forKey:@"cardCountVeryDay"];
                [[NSUserDefaults standardUserDefaults] setObject:currentDate forKey:@"cardSaveDate"];
            }
        }
        else
        {
            [[NSUserDefaults standardUserDefaults] setObject:currentDate forKey:@"cardSaveDate"];
        }
    }
    return YES;
}

- (BOOL)canShowInterstitial
{
    if ([WDBMSModel shareBMSModel].interConfig)
    {
        WDLog(@"WDShowInterstitialAd====:%@,%@",[WDBMSModel shareBMSModel].interConfig,[WDBMSModel shareBMSModel].interConfig.firstObject);
        if ([WDBMSModel shareBMSModel].interConfig.count <= 0)
        {
            return NO;
        }
        NSString *interstitialFirst = [NSString stringWithFormat:@"%@",[WDBMSModel shareBMSModel].interConfig.firstObject];
        NSString *interstitialSecond = [NSString stringWithFormat:@"%@",[WDBMSModel shareBMSModel].interConfig[1]];
        NSString *interstitialThird = [NSString stringWithFormat:@"%@",[WDBMSModel shareBMSModel].interConfig[2]];
        
        if (![self interstitialCountVeryDay])
        {
            //达到每日上限
            return NO;
        }
        
        if (self.interstitialAdCurrentCount == 0)
        {
            [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"saveInterstitialCount"];
            [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"InterstitialTargetCount"];
        }
        self.interstitialAdCurrentCount++;
        if ([interstitialFirst isEqualToString:@"1"])
        {
            NSString *saveInterstitialCount = [[NSUserDefaults standardUserDefaults] objectForKey:@"saveInterstitialCount"];
            if (saveInterstitialCount.length == 0)
            {
                WDLog(@"Interstitial ad 没有保存的参数，默认第一次");
                [[NSUserDefaults standardUserDefaults] setObject:@"1" forKey:@"saveInterstitialCount"];
                [[NSUserDefaults standardUserDefaults] setObject:[NSString stringWithFormat:@"%ld",[interstitialSecond integerValue]+1] forKey:@"InterstitialTargetCount"];
                if ([interstitialSecond integerValue] == 0)
                {
                    return YES;
                }
            }
            else{
                
                NSInteger newCount = [saveInterstitialCount integerValue]+1;
                [[NSUserDefaults standardUserDefaults] setObject:[NSString stringWithFormat:@"%ld",newCount] forKey:@"saveInterstitialCount"];
                NSString *currentInterstitialCount = [[NSUserDefaults standardUserDefaults] objectForKey:@"saveInterstitialCount"];
                NSString *interstitialTargetCount = [[NSUserDefaults standardUserDefaults] objectForKey:@"InterstitialTargetCount"];
                [[NSUserDefaults standardUserDefaults] setObject:[NSString stringWithFormat:@"%ld",[interstitialThird integerValue]+newCount+1] forKey:@"InterstitialTargetCount"];
                WDLog(@"当前是%@次，要在%ld次显示",currentInterstitialCount,[interstitialTargetCount integerValue]);
                if ([interstitialThird integerValue] == 0)
                {
                    WDLog(@"但interstitialThird间隔为0，所以需要显示");
                    return YES;
                }
                if ([interstitialTargetCount integerValue] == [currentInterstitialCount integerValue])
                {
                    
                    return YES;
                }
            }
        }
    }
    
    return NO;
}

- (BOOL)interstitialCountVeryDay
{
    NSString *interstitialFourth = [NSString stringWithFormat:@"%@",[WDBMSModel shareBMSModel].interConfig[3]];
    NSString *interstitialCountVeryDay = [[NSUserDefaults standardUserDefaults] objectForKey:@"interstitialCountVeryDay"];
    if (interstitialCountVeryDay.length > 0)
    {
        NSInteger newCount = [interstitialCountVeryDay integerValue]+1;
        [[NSUserDefaults standardUserDefaults] setObject:[NSString stringWithFormat:@"%ld",newCount] forKey:@"interstitialCountVeryDay"];
    }
    else{
        interstitialCountVeryDay = @"0";
        [[NSUserDefaults standardUserDefaults] setObject:@"1" forKey:@"interstitialCountVeryDay"];
    }
    if ([interstitialFourth integerValue] > 0)
    {
        
        NSTimeInterval time = [[NSDate date] timeIntervalSince1970];
        NSDate *date = [NSDate dateWithTimeIntervalSince1970:time];
        NSDateFormatter *objDateformat = [[NSDateFormatter alloc] init];
        [objDateformat setDateFormat:@"yyyyMMdd"];
        NSString *currentDate = [objDateformat stringFromDate:date];
        
        NSString *saveDate = [[NSUserDefaults standardUserDefaults] objectForKey:@"saveDate"];
        if (saveDate.length > 0)
        {
            WDLog(@"当前日期为%@,saveDate为%@,当前次数为%@,上限次数为%@",currentDate,saveDate,interstitialCountVeryDay,interstitialFourth);
            if ([saveDate integerValue] == [currentDate integerValue])
            {
                if ([interstitialCountVeryDay integerValue] <= [interstitialFourth integerValue])
                {
                    //还没到上限
                    return YES;
                }
                else
                {
                    return NO;
                }
            }else{
                //重置次数
                [[NSUserDefaults standardUserDefaults] setObject:@"0" forKey:@"interstitialCountVeryDay"];
                [[NSUserDefaults standardUserDefaults] setObject:currentDate forKey:@"saveDate"];
            }
        }
        else
        {
            [[NSUserDefaults standardUserDefaults] setObject:currentDate forKey:@"saveDate"];
        }
    }
    return YES;
}

- (BOOL)splashCountVeryDay
{
    NSString *splashFourth = [NSString stringWithFormat:@"%@",[WDBMSModel shareBMSModel].splashConfig[3]];
    NSString *splashCountVeryDay = [[NSUserDefaults standardUserDefaults] objectForKey:@"splashCountVeryDay"];
    if (splashCountVeryDay.length > 0)
    {
        NSInteger newCount = [splashCountVeryDay integerValue]+1;
        [[NSUserDefaults standardUserDefaults] setObject:[NSString stringWithFormat:@"%ld",newCount] forKey:@"splashCountVeryDay"];
    }
    else{
        splashCountVeryDay = @"0";
        [[NSUserDefaults standardUserDefaults] setObject:@"1" forKey:@"splashCountVeryDay"];
    }
    if ([splashFourth integerValue] > 0)
    {
        
        NSTimeInterval time = [[NSDate date] timeIntervalSince1970];
        NSDate *date = [NSDate dateWithTimeIntervalSince1970:time];
        NSDateFormatter *objDateformat = [[NSDateFormatter alloc] init];
        [objDateformat setDateFormat:@"yyyyMMdd"];
        NSString *currentDate = [objDateformat stringFromDate:date];
        
        NSString *saveDate = [[NSUserDefaults standardUserDefaults] objectForKey:@"splashSaveDate"];
        if (saveDate.length > 0)
        {
            WDLog(@"splash当前日期为%@,saveDate为%@,当前次数为%@,上限次数为%@",currentDate,saveDate,splashCountVeryDay,splashFourth);
            if ([saveDate integerValue] == [currentDate integerValue])
            {
                if ([splashCountVeryDay integerValue] <= [splashFourth integerValue])
                {
                    //还没到上限
                    return YES;
                }
                else
                {
                    return NO;
                }
            }else{
                //重置次数
                [[NSUserDefaults standardUserDefaults] setObject:@"0" forKey:@"splashCountVeryDay"];
                [[NSUserDefaults standardUserDefaults] setObject:currentDate forKey:@"splashSaveDate"];
            }
        }
        else
        {
            [[NSUserDefaults standardUserDefaults] setObject:currentDate forKey:@"splashSaveDate"];
        }
    }
    return YES;
}

- (BOOL)canShowSplash
{
    if ([WDBMSModel shareBMSModel].splashConfig)
    {
        WDLog(@"WDShowSplashAd====:%@,%@",[WDBMSModel shareBMSModel].splashConfig,[WDBMSModel shareBMSModel].splashConfig.firstObject);
        if ([WDBMSModel shareBMSModel].splashConfig.count <= 0)
        {
            return NO;
        }
        NSString *splashFirst = [NSString stringWithFormat:@"%@",[WDBMSModel shareBMSModel].splashConfig.firstObject];
        NSString *splashSecond = [NSString stringWithFormat:@"%@",[WDBMSModel shareBMSModel].splashConfig[1]];
        NSString *splashThird = [NSString stringWithFormat:@"%@",[WDBMSModel shareBMSModel].splashConfig[2]];
        
        if (![self splashCountVeryDay])
        {
            //达到每日上限
            return NO;
        }
        
        if ([splashFirst isEqualToString:@"1"])
        {
            NSString *saveSplashCount = [[NSUserDefaults standardUserDefaults] objectForKey:@"saveSplashCount"];
            if (saveSplashCount.length == 0)
            {
                WDLog(@"没有保存的参数，默认第一次");
                [[NSUserDefaults standardUserDefaults] setObject:@"1" forKey:@"saveSplashCount"];
                [[NSUserDefaults standardUserDefaults] setObject:[NSString stringWithFormat:@"%ld",[splashSecond integerValue]+1] forKey:@"SplashTargetCount"];
                if ([splashSecond integerValue] == 0)
                {
                    return YES;
                }
            }
            else{
                NSInteger newCount = [saveSplashCount integerValue]+1;
                [[NSUserDefaults standardUserDefaults] setObject:[NSString stringWithFormat:@"%ld",newCount] forKey:@"saveSplashCount"];
                NSString *currentSplashCount = [[NSUserDefaults standardUserDefaults] objectForKey:@"saveSplashCount"];
                NSString *splashTargetCount = [[NSUserDefaults standardUserDefaults] objectForKey:@"SplashTargetCount"];
                [[NSUserDefaults standardUserDefaults] setObject:[NSString stringWithFormat:@"%ld",[splashThird integerValue]+newCount+1] forKey:@"SplashTargetCount"];
                WDLog(@"当前是%@次，要在%ld次显示",currentSplashCount,[splashTargetCount integerValue]);
                if ([splashThird integerValue] == 0)
                {
                    WDLog(@"但splashThird间隔为0，所以需要显示");
                    return YES;
                }
                if ([splashTargetCount integerValue] == [currentSplashCount integerValue])
                {
                    
                    return YES;
                }
                
            }
            
        }
    }
    
    return NO;
}

@end
